package com.zxy.ziems.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.btp.common.response.JsonResponse;
import com.zxy.ziems.server.domain.Dict;
import com.zxy.ziems.server.dto.DictDto;
import com.zxy.ziems.server.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 所有字典
 *
 * @author bhg
 * @date 2023/3/13
 */
@RestController
@RequestMapping("/api/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 字典列表
     *
     * @return
     */
    @GetMapping("/list")
    public JsonResponse<List<Dict>> list() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().orderByAsc(Dict::getDictName);
        List<Dict> list = dictService.list(queryWrapper);
        return JsonResponse.SUCCESS(list);
    }

    /**
     * 字典新增修改
     *
     * @param dictDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse addOrUpdate(@RequestBody @Validated DictDto dictDto) {
        dictService.addOrUpdate(dictDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 字典删除
     *
     * @param dictId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(@RequestParam(value = "dictId") String dictId) {
        dictService.removeById(dictId);
        return JsonResponse.SUCCESS();
    }
}
